/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NOTE: component c[3]/w is ignored for the time being in all calculations.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

// INCLUDES -------------------------------------------------------------------------
#include "raytracer.h"  // project

#include "stdio.h"  		// printf()
#include "string.h" 		// mem[set/cpy/cmp]()

// FUNCTIONS ------------------------------------------------------------------------

// printf() for ray
void rayPrintf(ray* r)
{
	NULL_GUARD(r);

	MSG("o:[%.3f %.3f %.3f %.3f], d:[%.3f %.3f %.3f %.3f]\n",
	    r->o.x, r->o.y, r->o.z, r->o.w,
		r->d.x, r->d.y, r->d.z, r->d.w);
};

// clear the ray to o:[0,0,0,1], d:[0,0,-1,1]
void rayClear(ray* r)
{
	NULL_GUARD(r);

	vectorClear(&r->o); // set origin to [0,0,0,1]

	// set direction to [0,0,-1,1]
	vectorClear(&r->d);
	r->d.z = -1.0f;
};

// copy ray a to r. -> r = a
void raySet(ray* r, ray* a)
{
	NULL_GUARD(r);
	NULL_GUARD(a);

	// copy vector contents
	memcpy(r, a, sizeof(ray));
};

/* the vector equation of a line through some point o and parallel to
   another vector d is defined as:

   p = o + td

   where:

   o    is the origin of the line
   t    being a scalar
   d    a vector parallel to the line

   resulting in every real value of scalar t generating a real point p on the line. */

// retrieve an arbitrary point p on the line produced by scalar t
void rayPoint(vector* p, ray* r, fNumber t)
{
	NULL_GUARD(r);

	// NULL_GUARD(p) is in vectorMultiply()
	// p = t*direction
	vectorMultiply(p, &r->d, t);
	// p += origin
	vectorAddSelf(p, &r->o);
};
